package com.meixiang.main;

import android.annotation.TargetApi;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.util.DensityUtil;
import com.meixiang.view.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 *用于viewPager包裹Fragment的Activity。实现 三个抽象方法即可。不需设置布局
 */

public abstract class BaseViewPagerActivity extends BaseActivity {
    @Bind(R.id.ll_background)
    LinearLayout mLlBackground;

    @Bind(R.id.ll_tab)
    LinearLayout llTab;
    @Bind(R.id.fl_mTabStrip_item)
    FrameLayout flMTabStripItem;
    @Bind(R.id.ll_mTabStrip)
    LinearLayout llMTabStrip;
    @Bind(R.id.viewpager)
    CustomViewPager viewpager;
    @Bind(R.id.view_style)
    View viewStyle;
    private String[] titles;
    private Fragment[] fragments;
    private List<TextView> viewList;

    //添加颜色样式
    private int textSize=15;
    //选中颜色
    private int ColorStyle;
    //未选中颜色
    protected int normalTextColor=R.color.black_8;;

    private boolean isLine=true;

    private int background=R.color.white; //背景颜色
    protected abstract String[] getTitles();
    protected abstract Fragment[] getFragments();

    protected abstract int ColorStyle();

    protected abstract void setTitle();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_base_view_pager);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setTitle();
    }

    protected void setTextNormalColor(){

    }

    @Override
    protected void initView() {
        mLlBackground.setBackgroundResource(background);
        viewList = new ArrayList<>();
        titles = getTitles();
        ColorStyle = ColorStyle();
        fragments = getFragments();
        for (int i = 0; i < titles.length; i++) {
            if (i > 0 && isLine) {
                View view = new View(this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(1, LinearLayout.LayoutParams.MATCH_PARENT);
                params.topMargin = DensityUtil.dip2px(this, 10);
                params.bottomMargin = DensityUtil.dip2px(this, 10);
                view.setLayoutParams(params);
                view.setBackgroundColor(ContextCompat.getColor(this, R.color.line_color));
                llTab.addView(view);
            }
            TextView tv = new TextView(this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
            tv.setLayoutParams(layoutParams);
            tv.setGravity(Gravity.CENTER);
            tv.setText(titles[i]);

//            tv.setTextColor(ColorStyle);
            //初始设置第一个为选中颜色
            if (i == 0) {
                tv.setTextColor(ContextCompat.getColor(this, ColorStyle));
                tv.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                tv.setTextColor(ContextCompat.getColor(this,normalTextColor));
                tv.setTypeface(Typeface.DEFAULT);
            }
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);

            final int finalI = i;
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewpager.setCurrentItem(finalI);
                }
            });
            llTab.addView(tv);
            viewList.add(tv);
        }
        llMTabStrip.setWeightSum(titles.length);
        viewStyle.setBackgroundColor(ContextCompat.getColor(this, ColorStyle));
        viewpager.setOffscreenPageLimit(3);
        viewpager.setAdapter(new PagerAdapter(fragments,this.getSupportFragmentManager()));
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                flMTabStripItem.setTranslationX(viewList.get(position).getWidth() * position + positionOffsetPixels / viewList.size());
            }

            @Override
            public void onPageSelected(int position) {
                initColor();
                viewList.get(position).setTextColor(ContextCompat.getColor(activity, ColorStyle));
                viewList.get(position).setTypeface(Typeface.DEFAULT_BOLD);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void initColor(){
        for (TextView view:viewList){
            view.setTextColor(ContextCompat.getColor(this,normalTextColor));
            view.setTypeface(Typeface.DEFAULT);
        }
    }
    /**
     *
     * 设置是否有分割线
     * @param isLine
     */
    protected void isCuttingLine(boolean isLine) {
        this.isLine = isLine;
    }
    public void setViewpagerSlide(boolean isSlide){
        viewpager.setScanScroll(isSlide);
    }
    @Override
    protected void setData() {

    }

    /**
     * 设置字体大小
     * @param textSize
     */
    protected void setTextSize(int textSize) {
        this.textSize=textSize;
    }
    /**
     * onCreateView 之后调用
     * 设置文字
     */
    protected void setTabText(int position,String text){
        viewList.get(position).setText(text);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    class PagerAdapter extends FragmentPagerAdapter {
        private Fragment[] fragments;
        public PagerAdapter(Fragment[] fragments,FragmentManager fm) {
            super(fm);
            this.fragments=fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }
    }
}
